package com.cup.bddm.impala.controller;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cup.bddm.handler.Token;
import com.cup.bddm.impala.Constant;
import com.cup.bddm.impala.po.StreetInfo;
import com.cup.bddm.impala.service.StreetService;
import com.cup.bddm.impala.vo.MchntInterTopVo;
import com.cup.bddm.impala.vo.StreetAppListVo;
import com.cup.bddm.impala.vo.StreetDailyLiveMchntVo;
import com.cup.bddm.impala.vo.StreetInfoVo;
import com.cup.bddm.impala.vo.StreetMchntListVo;
import com.cup.bddm.impala.vo.StreetMonthTransVo;
import com.cup.bddm.impala.vo.StreetTransDistVo;
import com.cup.bddm.impala.vo.StreetTransInfoVo;
import com.cup.bddm.impala.vo.StreetUpdateVo;
import com.cup.bddm.util.DateUtil;

@Token
@Controller("srv-StreetController")
@RequestMapping(value = "/services/street")
public class StreetController {

	static Logger logger = Logger.getLogger(StreetController.class);

	@Autowired
	StreetService streetService;

	@RequestMapping(value = "/trans/{streetCd}/{date}", method = RequestMethod.GET)
	@ResponseBody
	public StreetTransInfoVo getStreetTrans(@PathVariable("streetCd") String streetCd, @PathVariable("date") String date) {
		logger.info("street abstract info request:streetCd=" + streetCd + ", date" + date);

		StreetTransInfoVo result = new StreetTransInfoVo();

		if (StringUtils.isEmpty(streetCd) || StringUtils.isEmpty(date)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}

		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}
		result = streetService.getStreetTrans(streetCd, date);
		return result;
	}

	@RequestMapping(value = "/abs/{streetCd}", method = RequestMethod.GET)
	@ResponseBody
	public StreetInfoVo getStreetInfo(@PathVariable("streetCd") String streetCd) {
		logger.info("street info request:streetCd=" + streetCd);

		StreetInfoVo result = new StreetInfoVo();

		if (StringUtils.isEmpty(streetCd)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}
		result = streetService.getStreetInfo(streetCd);
		return result;
	}

	@RequestMapping(value = "/transTpDis/{streetCd}/{date}", method = RequestMethod.GET)
	@ResponseBody
	public StreetTransDistVo getStreetTransByTransType(@PathVariable("streetCd") String streetCd, @PathVariable("date") String date) {
		logger.info("street trans type trans distribute request:streetCd=" + streetCd + ", date" + date);
		StreetTransDistVo result = new StreetTransDistVo();

		if (StringUtils.isEmpty(streetCd) || StringUtils.isEmpty(date)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}
		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}
		result = streetService.getStreetTransByTransType(streetCd, date);

		return result;
	}

	@RequestMapping(value = "/mchntTpDis/{streetCd}/{date}", method = RequestMethod.GET)
	@ResponseBody
	public StreetTransDistVo getStreetTransByMchntType(@PathVariable("streetCd") String streetCd, @PathVariable("date") String date) {
		logger.info("street mchnt type  trans distribute request:streetCd=" + streetCd + ", date" + date);
		StreetTransDistVo result = new StreetTransDistVo();

		if (StringUtils.isEmpty(streetCd) || StringUtils.isEmpty(date)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}
		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}

		result = streetService.getStreetTransByMchntType(streetCd, date);

		return result;
	}

	@RequestMapping(value = "/transTrend/{streetCd}/{date}", method = RequestMethod.GET)
	@ResponseBody
	public StreetMonthTransVo getRecentMonthsTrans(@PathVariable("streetCd") String streetCd, @PathVariable("date") String date) {
		logger.info("street recent month trans request:streetCd=" + streetCd + ", date" + date);

		StreetMonthTransVo result = new StreetMonthTransVo();

		if (StringUtils.isEmpty(streetCd) || StringUtils.isEmpty(date)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}
		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}

		result = streetService.getRecentMonthsTrans(streetCd, date);
		return result;
	}

	@RequestMapping(value = "/bizTrend/{streetCd}/{date}", method = RequestMethod.GET)
	@ResponseBody
	public StreetMonthTransVo getRecentMonthsTpTrans(@PathVariable("streetCd") String streetCd, @PathVariable("date") String date) {
		logger.info("street recent month tp trans request:streetCd=" + streetCd + ", date" + date);
		StreetMonthTransVo result = new StreetMonthTransVo();

		if (StringUtils.isEmpty(streetCd) || StringUtils.isEmpty(date)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}
		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}

		result = streetService.getRecentMonthsTpTrans(streetCd, date);

		return result;
	}

	@RequestMapping(value = "/activeTrend/{streetCd}/{date}", method = RequestMethod.GET)
	@ResponseBody
	public StreetMonthTransVo getRecentMonthsActiveMchntNum(@PathVariable("streetCd") String streetCd, @PathVariable("date") String date) {
		logger.info("street recent month active mchnt num request:streetCd=" + streetCd + ", date" + date);
		StreetMonthTransVo result = new StreetMonthTransVo();

		if (StringUtils.isEmpty(streetCd) || StringUtils.isEmpty(date)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}
		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}

		result = streetService.getRecentMonthsActiveMchntNum(streetCd, date);

		return result;
	}

	@RequestMapping(value = "/liveMchnt/{streetCd}/{date}", method = RequestMethod.GET)
	@ResponseBody
	public StreetDailyLiveMchntVo getLiveMchntNum(@PathVariable("streetCd") String streetCd, @PathVariable("date") String date) {
		logger.info("street daily live mchnt num request:streetCd=" + streetCd + ", date" + date);
		StreetDailyLiveMchntVo result = new StreetDailyLiveMchntVo();

		if (StringUtils.isEmpty(streetCd) || StringUtils.isEmpty(date)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}
		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}

		result = streetService.getLiveMchntNum(streetCd, date);

		return result;
	}

	@RequestMapping(value = "/declineMchnt/{streetCd}/{type}/{date}", method = RequestMethod.GET)
	@ResponseBody
	public StreetMchntListVo getTopFallMchnt(@PathVariable("streetCd") String streetCd, @PathVariable("type") String type, @PathVariable("date") String date) {
		logger.info("street top decline mchnt request:streetCd=" + streetCd + ",type=" + type + ", date" + date);
		StreetMchntListVo result = new StreetMchntListVo();

		if (StringUtils.isEmpty(streetCd) || StringUtils.isEmpty(date) || StringUtils.isEmpty(type)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}
		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}

		if (!type.equals("nfc") && !type.equals("qrc")) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}

		result = streetService.getTopFallMchnt(streetCd, type, date);

		return result;
	}

	@RequestMapping(value = "/lessMchnt/{streetCd}/{type}/{date}", method = RequestMethod.GET)
	@ResponseBody
	public StreetMchntListVo getLessThan5Mchnt(@PathVariable("streetCd") String streetCd, @PathVariable("type") String type, @PathVariable("date") String date) {
		logger.info("street top less than 5 trans mchnt request:streetCd=" + streetCd + ",type=" + type + ", date" + date);
		StreetMchntListVo result = new StreetMchntListVo();

		if (StringUtils.isEmpty(streetCd) || StringUtils.isEmpty(date) || StringUtils.isEmpty(type)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}
		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}

		if (!type.equals("nfc") && !type.equals("qrc")) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}

		result = streetService.getLessThan5Mchnt(streetCd, type, date);

		return result;
	}

	@RequestMapping(value = "/appRank/{streetCd}/{date}", method = RequestMethod.GET)
	@ResponseBody
	public StreetAppListVo getSortedAppTrans(@PathVariable("streetCd") String streetCd, @PathVariable("date") String date) {
		logger.info("street app trans rank  request:streetCd=" + streetCd + ", date" + date);
		StreetAppListVo result = new StreetAppListVo();

		if (StringUtils.isEmpty(streetCd) || StringUtils.isEmpty(date)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}
		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}

		result = streetService.getSortedAppTrans(streetCd, date);

		return result;
	}

	@RequestMapping(value = "/topMchnt/{streetCd}/{type}/{date}", method = RequestMethod.GET)
	@ResponseBody
	public MchntInterTopVo getTop5Mchnt(@PathVariable("streetCd") String streetCd, @PathVariable("type") String type, @PathVariable("date") String date) {

		logger.info("street top mchnt request:streetCd=" + streetCd + ",type=" + type + ", date" + date);
		MchntInterTopVo result = new MchntInterTopVo();

		if (StringUtils.isEmpty(streetCd) || StringUtils.isEmpty(date) || StringUtils.isEmpty(type)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}
		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}

		if (!type.equals("nfc") && !type.equals("qrc") && !type.equals("offline")) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}

		result = streetService.getTop5Mchnt(streetCd, type, date);

		return result;
	}

	/**
	 * 功能：PC端商圈录入接口（用于存量数据录入）
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public StreetUpdateVo updateStreetInfo(List<StreetInfo> json) {
		logger.info("insert street info: size=" + json.size());
		StreetUpdateVo result = new StreetUpdateVo();

		if (json.size() == 0) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}

		result = streetService.updateStreets(json);

		return result;
	}

	/**
	 * 功能：移动端商圈录入接口（用于增量数据录入）
	 */
	@RequestMapping(value = "/update_m", method = RequestMethod.POST, consumes = "application/json; charset=UTF-8")
	@ResponseBody
	public StreetUpdateVo updateStreetInfo_m(List<StreetInfo> json) {
		logger.info("insert street info: size=" + json.size());
		StreetUpdateVo result = new StreetUpdateVo();

		if (json.size() == 0) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}
		// 处理业务street_cd:2900S+流水号
		// 1.生成商圈编号

		result = streetService.updateStreets_m(json);

		return result;
	}

	/**
	 * http://127.0.0.1:8081/BizAreaService/services/street/serial/00011000
	 * 根据商圈ＩＤ前五位数，生成新的商圈ＩＤ
	 */
	@RequestMapping(value = "/serial/{biz_street_cd}")
	@ResponseBody
	public String getMaxStreetCd(@PathVariable("biz_street_cd") String biz_street_cd) {
		return streetService.getMaxStreetCd(biz_street_cd);
	}

}
